/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FramePrincipal.java
 *
 * Created on 07/12/2011, 18:33:49
 */

package br.com.projetoObras.view;

import br.com.projetoObras.controller.ArtistController;
import com.towel.swing.img.JImagePanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 *
 * @author Tiago
 */
public class MainFrame extends javax.swing.JFrame {
    private JMenu menu_search;
    private JMenuItem menu_artist_search;

    /** Creates new form FramePrincipal */
    public MainFrame() throws IOException, URISyntaxException {
        iniciarComponentes();


        //getContentPane().removeAll();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 599, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 337, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void iniciarComponentes() throws IOException, URISyntaxException{
        //this.setBackground(Color.red);
        this.setExtendedState(this.MAXIMIZED_BOTH);
        System.out.println("Height: "+this.getBounds().getHeight()+"  Width: "+this.getBounds().getWidth());
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
        //Colocar icone correto na janela!

        menuBar = new JMenuBar();
        menu_file = new JMenu();
        menu_edit = new JMenu();
        menu_new = new JMenu();
        menu_search = new JMenu();
        menu_report = new JMenu();
        menu_exit = new JMenuItem();

        menu_begin = new JMenuItem();
        menu_artist = new JMenuItem();
        menu_sell_report = new JMenuItem();
        menu_cost_report = new JMenuItem();
        menu_artist = new JMenuItem();
        menu_work = new JMenuItem();
        
        menu_search.setText("Buscar");
        
        menu_artist_search = new JMenuItem();
        menu_artist_search.setText("Artista");

        menu_artist_edit = new JMenuItem();
        menu_registry_edit = new JMenuItem();
        menu_work_edit = new JMenuItem();

        lbl_image = new JLabel();

        menu_file.setText("Arquivo");
        menu_edit.setText("Editar");
        menu_new.setText("Novo");
        menu_begin.setText("Início");
        menu_report.setText("Relatórios");
        menu_exit.setText("Sair");

        menu_artist.setText("Artista");
        menu_sell_report.setText("Vendas");
        menu_cost_report.setText("Gastos");
        menu_work.setText("Obra");



        menu_artist_edit.setText("Artista");
        menu_registry_edit.setText("Relatório");
        menu_work_edit.setText("Obra");

        //adiciona itens aos menus
        menu_file.add(menu_begin);
        menu_file.add(menu_new);
        menu_file.add(menu_edit);
        menu_file.add(menu_search);
        menu_file.add(menu_exit);
        menu_report.add(menu_sell_report);
        menu_report.add(menu_cost_report);
        menu_new.add(menu_artist);
        menu_new.add(menu_work);
        
        menu_search.add(menu_artist_search);

        menu_edit.add(menu_artist_edit);
        menu_edit.add(menu_work_edit);


        //adicionar menus ao menuBar

        menuBar.add(menu_file);
        menuBar.add(menu_report);
        this.getContentPane().add(menuBar,BorderLayout.NORTH);

        lbl_image.setBounds(this.getBounds());
        System.out.println(lbl_image.getSize());
        
        //lbl_image.setIcon(new ImageIcon(getClass().getResource("\\2.jpg")));  
        //getContentPane().add(lbl_image,BorderLayout.CENTER);
        //System.out.println(this.getClass().getResource("background.jpg").toString());
        //String img = this.getClass().getResource("background.jpg").toString();
        //System.out.println(img);
        //new File
        other_panel = new JImagePanel(new File(this.getClass().getResource("background.jpg").toURI()));
        //other_panel.setSize(new Dimension(this.getWidth(), this.getHeight()));
        // other_panel.setLayout(BorderLayout.LINE_START);
        this.getContentPane().add(other_panel, BorderLayout.CENTER);

        //other_panel.setSize(new Dimension(this.getWidth(), this.getHeight()));
        other_panel.setBounds(this.getBounds());
       
        new_panel();
        //getContentPane().add(lbl_image,BorderLayout.CENTER);
        repaint();
        show();

        //Eventos menu
        menu_artist.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    menu_artist_clicked(e);
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex){
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        });

        menu_begin.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    menu_begin_clicked(e);
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        });

        menu_work.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    menu_work_clicked(e);
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        });

      

        menu_sell_report.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
              menu_reportVendas_clicked(e);
            }


        });

        menu_cost_report.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
              menu_reportGastos_clicked(e);
            }


        });


        menu_artist_edit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
              menu_artist_edit_clicked(e);
            }


        });

        menu_work_edit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
              menu_work_edit_clicked(e);
            }


        });
        
        menu_artist_search.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    menu_artist_search_clicked(e);
                } catch (SQLException ex){
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex){
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        });
        
        menu_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                menu_exit_clicked(e);
            }
        });

    }

    private void menu_reportVendas_clicked(ActionEvent e) {
        this.clearPanel();
        //report = new Visaoreport(this,0);
        //this.getContentPane().add(report,BorderLayout.CENTER);
        repaint();
        this.show();
    }

    private void menu_reportGastos_clicked(ActionEvent e) {
        this.clearPanel();
        //report = new Visaoreport(this,1);
        //this.getContentPane().add(report,BorderLayout.CENTER);
        repaint();
        this.show();
    }

    private void menu_work_edit_clicked(ActionEvent e) {
        this.clearPanel();
        //work = new Visaowork(this, true);
        //this.getContentPane().add(work,BorderLayout.CENTER);
        repaint();
        this.show();
    }
    
    private void menu_artist_search_clicked(ActionEvent e) throws SQLException, IOException {
        artist = new ArtistSearchView(this, new ArtistController());
        new_panel();
        main_panel.add(artist,BorderLayout.CENTER);

        repaint();
        this.show();
    }
    
    private void menu_artist_edit_clicked(ActionEvent e){
        this.clearPanel();
        
        //this.getContentPane().add(artist,BorderLayout.CENTER);
        repaint();
        this.show();
    }
    
    
    private void menu_begin_clicked(ActionEvent e) throws IOException {


        new_panel();
        //artist = new Visaoartist(this);
        //artist.setBounds(this.getBounds());
        this.getContentPane().add(lbl_image,BorderLayout.CENTER);
        repaint();
        this.show();
    }

    private void menu_artist_clicked(ActionEvent e) throws IOException, SQLException {

        artist= new ArtistRegistryView(new ArtistController(), this);
        new_panel();
        main_panel.add(artist,BorderLayout.CENTER);

        repaint();
        this.show();
    }

    private void menu_work_clicked(ActionEvent e) throws IOException {

        work = new WorkRegistryView(this);
        new_panel();
        main_panel.add(work,BorderLayout.WEST);

        

        repaint();
        this.show();
    }
    
    
    private void menu_exit_clicked(ActionEvent e) {
        this.dispose();
    }

    

     
    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
 

    private JMenuBar menuBar;
    private JMenu menu_file;
    private JMenu menu_edit;
    private JMenu menu_new;
    private JMenu menu_report;
    private JMenuItem menu_exit;
    private JMenuItem menu_begin;
    private JMenuItem menu_artist;
    private JMenuItem menu_sell_report;
    private JMenuItem menu_work;
    private JMenuItem menu_artist_edit;
    private JMenuItem menu_registry_edit;
    private JMenuItem menu_work_edit;
    private JMenuItem menu_cost_report;
    private JLabel lbl_image;
    //private JPanel main_panel;
    //private UJPanelImagem main_panel;
    private JPanel main_panel;
    private JImagePanel other_panel;

    private JPanel artist;
    private WorkRegistryView work;
    
    
    public void clearPanel(){
    
        List<Component> componentes = Arrays.asList(this.getContentPane().getComponents());

        int i;
        main_panel.remove(work);
        main_panel.remove(artist);
        
    }
    
    public JPanel getMain_panel(){
        return main_panel;
    }
    
    
    public void new_panel() throws IOException{
        if(main_panel != null)
            other_panel.remove(main_panel);
            //this.getContentPane().remove(main_panel);
        main_panel = new JPanel();
        main_panel.setOpaque(true);
        other_panel.setLayout(new SpringLayout());
        //other_panel.getLayout().
        //main_panel = new JImagePanel(new File("C:\\Users\\Public\\2.jpg"));
        //main_panel.setSize(new Dimension(this.getWidth(), this.getHeight()));
        //main_panel.setImagem(new File(getClass().getResource("\\2.jpg").toString()));
        //main_panel.setOpaque(true);
        //main_panel.repaint();
        //main_panel.show();
        other_panel.add(main_panel, BorderLayout.LINE_START);
        //this.getContentPane().add(main_panel, BorderLayout.WEST);
    }

}
